﻿
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}

<!--页面主要内容-->
<main class="lyear-layout-content">

    <div class="container-fluid">

        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-toolbar clearfix">
                        <div class="toolbar-btn-action">
                            <a class="btn btn-primary m-r-5" data-toggle="modal" href="#modelusers"><i class="mdi mdi-plus"></i> 新增</a>
                            @*<a class="btn btn-success m-r-5" href="#!"><i class="mdi mdi-check"></i> 启用</a>
            <a class="btn btn-warning m-r-5" href="#!"><i class="mdi mdi-block-helper"></i> 禁用</a>
        <a class="btn btn-danger" href="#!"><i class="mdi mdi-window-close"></i> 删除</a>*@

                            <div class="col-lg-4">
                                <div class="input-group">
                                    <input type="text" class="form-control" id="keyword" name="keyword" placeholder="请输入赛区...">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default" type="button" onclick="search()">搜索</button>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <div style="width:50%">
                            <div class="table-responsive">
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th>
                                                <label class="lyear-checkbox checkbox-primary">
                                                    <input type="checkbox" id="check-all"><span></span>
                                                </label>
                                            </th>
                                            <th>编号</th>
                                            <th>赛区</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody id="body"></tbody>
                                </table>
                            </div>
                            <ul class="pagination" id="page"></ul>

                        </div>
                        </div>
                </div>
            </div>

        </div>

    </div>

</main>
<!--End 页面主要内容-->
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" id="modelusers" aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="myLargeModalLabel">新增赛区</h4>
            </div>
            <form class="form-horizontal" id="form1" action="/AdminUser/add" method="post">
                <div class="modal-body">
                    <input type="hidden" id="ID" name="ID" value="0">
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="AreaName">赛事区域</label>
                        <div class="col-md-7">
                            <input class="form-control" type="text" id="AreaName" name="AreaName" placeholder="请输入赛区..">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary" onclick="sava()">点击保存</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>


    $(function () {
        $("#admin").addClass("active").siblings().removeClass("active");
        $("#AArea").addClass("active").siblings().removeClass("active");
        getPageOfData(1);  //初始化页面
    })
    $('#modelusers').on('hidden.bs.modal', function () {
       
        $(':input', '#form1').not(':button,:submit,:reset,#ID').val('').removeAttr('checked').removeAttr('checked');
    });
    function setModel(obj) {
        $("#AreaName").val(obj);
    }

    function search() {
        getPageOfData(1);
    }

    function del(id) {

            $.alert({
                title: '提示框',
                content: '<br> 确定 <strong>删除?</strong>',
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'btn-primary',
                        action: function () {
                           
                            $.ajax({
                                url: "/Area/del",
                                data: { id: id },
                                dataType: "json",
                                success: function (data) {
                                    if (data != null) {

                                        getPageOfData(1);
                                    }
                                }
                            })
                        }
                    },
                    cancel: {
                        text: '取消',
                        action: function () {
                            //$.alert('你点击了取消!');
                        }
                    }
                }
            });
       

       
    }

    function getPageOfData(page) {

        var pageSize = 10;
        var keyword = $("#keyword").val();

        $.ajax({
            url: "/Area/GetLists",
            data: {
                pageIndex: page,  //页数
                pageSize: pageSize,  //每页几条
                keyword: keyword,

            },
            dataType: "json",
            //async: false,
            success: function (data) {
                var result = data.dt.length;
                var dt = data.dt;
                var strHtml = "";
                for (var i = 0; i < dt.length; i++) {
                    var temp = dt[i];
                    strHtml = strHtml + '<tr><td><label class="lyear-checkbox checkbox-primary"><input type="checkbox" name="ids[]" value="' + temp.ID + '"><span></span></label></td>';
                    strHtml = strHtml + '<td>' + temp.ID + '</td>';
                    strHtml = strHtml + '<td>' + temp.AreaName + '</td>';
                    strHtml = strHtml + "<td><div class='btn-group'><a class='btn btn-xs btn-default edit' data-toggle='modal' href='#modelusers'  onclick='setModel("+JSON.stringify(temp.AreaName)+")'  title='编辑' ><i class='mdi mdi-pencil'></i></a>";
                    strHtml = strHtml + '<a class="btn btn-xs btn-default" href="#!" title="删除"  onclick="del(' + temp.ID + ')" data-toggle="tooltip"><i class="mdi mdi-window-close"></i></a>';
                    strHtml = strHtml + '</div></td></tr>';
                }
                $('#body').html("");
                $('#body').html(strHtml);
                var totalPages = data.ToalPage;// Math.ceil(data.records / pageSize); //条数除以页数，是总页数
                var element = $('#page');  //ul的id
                var options = {
                    bootstrapMajorVersion: 3,
                    currentPage: page, // 当前页数
                    numberOfPages: 10, // 显示按钮的数量
                    totalPages: totalPages, // 总页数
                    itemTexts: function (type, page, current) {
                        switch (type) {
                            case "first":
                                return "首页";
                            case "prev":
                                return "上一页";
                            case "next":
                                return "下一页";
                            case "last":
                                return "末页";
                            case "page":
                                return page;
                        }
                    },
                    // 点击事件，用于通过Ajax来刷新整个list列表
                    onPageClicked: function (event, originalEvent, type, page) {
                        getPageOfData(page);
                    }
                };

                element.bootstrapPaginator(options);



            }
        }
        );
    };




    function sava() {
        var options = {
            url: "/Area/AddModel",
            type: "post",
            success: function (data) {
                if (data == "1") {
                    lightyear.notify('操作成功~', 'success', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');
                    getPageOfData(1);
                    $('#modelusers').modal('hide');
                }
                else if (data == "-1") {
                    lightyear.notify('操作失败，存在相同的赛区~', 'warning', 3000, 'mdi mdi-emoticon-neutral', 'top', 'center');
                }
                else {
                    lightyear.notify('操作失败，请联系管理员~', 'warning', 3000, 'mdi mdi-emoticon-neutral', 'top', 'center');
                }
                return false
            }
        };


        $('#form1').bootstrapValidator({
            live: 'enabled', //enabled代表当表单控件内容发生变化时就触发验证，默认提交时验证，
            // disabled和submitted代表当点击提交按钮时触发验证

            feedbackIcons: {  //显示表单验证结果的图标
                //valid: 'glyphicon glyphicon-ok',
                //invalid: 'glyphicon glyphicon-remove',
                //validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                AreaName: {
                    validators: {
                        notEmpty: {
                            message: '赛区不能为空'
                        }
                    }
                }
              
            }
        })
        $("#form1").ajaxForm(options);
    }
</script>
